<?php
	include "include/common.inc.php";
	
	$currUser = getLoginUser();
	
	if($currUser->getValue("id") == -1)
		goToUrl("login.php");
	
	$start=isset($_GET["start"])?(int)$_GET["start"]:0;
	$order=isset($_GET["order"])?$_GET["order"]:"`id` DESC";
	
	$postId=$_GET["postId"];
	
	function showComment($comment, $user, $start, $order){
		$author = User::getUser($comment->getValue("authorId"));
		$post = Post::getPost($comment->getValue("parentPostId"));
		echo "<tr>";
		echo "<td>";
		echo $comment->getValue("content");
		echo "</td>";
		
		echo "<td>";
		if($user->getValue("id") == $author->getValue("id")) {
			echo "<a href=\"editComment.php?commentId=".$comment->getValue("id")."\">Edit</a><br>";
			echo "<a href=\"?postId=".$post->getValue("id")."&delete=".$comment->getValue("id")."&start=$start&amp;order=$order\">Delete</a><br>"; }
		echo "By <a href=\"user.php?userId=".$comment->getValue("authorId")."\">".$author->getValue("userName")."</a><br>".$comment->getValue("timestamp")."</td>";
		echo "</tr>";
	}
	
	$post=Post::getPost($postId);
	
	if(isset($_POST["submit"])){
		$currUser->addComment($postId,htmlspecialchars_decode($_POST["content"]));
	}
	
	if(isset($_POST["EditBtn"])){
		$author = User::getUser($post->getValue("authorId"));
		$groupId = $post->getValue("groupId");
		if($currUser->getValue("id") == $author->getValue("id")){
			goTourl("editPost.php?postId=".$postId);
		}
	}
	
	if(isset($_POST["delete"])){
		$author = User::getUser($post->getValue("authorId"));
		$groupId = $post->getValue("groupId");
		if($currUser->getValue("id") == $author->getValue("id")){
			$post->delete();
			goToUrl("group.php?groupId=".$groupId);
		}
	}
	
	if(isset($_GET["delete"])){
		$comment = Comment::getComment($_GET["delete"]);
		$author = User::getUser($comment->getValue("authorId"));
		if($currUser->getValue("id") == $author->getValue("id")){
			$comment->delete();
		}
	}


?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Blind Dating</title>
	<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
    <!-- Bootstrap -->
    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="main.css" rel="stylesheet">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	<script type= "text/javascript" src = "js/countries2.js"></script>
    <script src="include/ckeditor/ckeditor.js"></script>
  </head>
  <body>
  
  <nav class="navbar navbar-default " role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header" >
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="./index.php">NUS Blind Dating Site</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="./index.php">Home</a></li>
        <li><a href="About.php">About</a></li>
        <li><a href="groups.php">Groups</a></li>
	
		
      
      </ul>
      <form class="navbar-form navbar-right" role="search" action="search_group.php" method="get">
        <div class="form-group">
          <input name ="search" type="text" class="form-control" placeholder="Search Group">
        </div>
        <button type="submit" class="btn btn-default" name="submit" value="submit">Search</button>
      </form>
	  <ul class="nav navbar-nav navbar-right">
	  <?php if ($currUser->getValue("id")==-1) {?>
        <li><a href="Register.php">Register</a></li>
        <li><a href="Login.php">Login</a></li>
      
	  <?php }else{ ?>
	  
	   <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Hello! <?php echo $currUser->getValue("userName"); ?> <b class="caret"></b></a>
          <ul class="dropdown-menu">
		  <li><a href="Myprofile.php">View My Profile</a></li>
		<li><a href="Editprofile.php">Edit My Profile</a></li>
        <li><a href="Groups.php">SeeGroup</a></li>
		<li><a href="Logout.php">Logout</a></li>
          </ul>
		  
        </li>
	  <?php }?>
	  </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

<div id="wrapper">
<table class="table">
<form action="post.php?postId=<?php echo $postId?>" method="POST" name="like">
<?php
    $currUser = getLoginUser();
	$author = User::getUser($post->getValue("authorId"));
	echo "<tr>";
	echo "<td width=\"70%\"><a href=\"post.php?postId=".$post->getValue("id")."\">".$post->getValue("title")."</a></td>";
	echo "<td width=\"20%\" rowspan=\"2\">By <a href=\"user.php?userId=".$post->getValue("authorId")."\">".$author->getValue("userName")."</a><br>".$post->getValue("timestamp")."</td>";
	
    if($currUser->getValue("id") == $author->getValue("id")) {
	echo "<td rowspan=\"2\"><input type=\"submit\" name=\"EditBtn\" value = \"Edit\">"; 
	echo "<input type=\"submit\" name=\"delete\" value = \"Delete\">";
	echo "</td>";
	}
	echo "</tr>";
	echo "<tr>";
	echo "<td>".$post->getValue("content")."</td>";
	
	echo "</tr>";
?>
</table>

<table class="table table-striped">
<?php 
	list($comments, $totalRows) = $post->getComments($start,PAGE_SIZE,$order);
	
	if($totalRows > 0){
		echo "<h2>Comments</h2>";
		echo "<tr><th width=\"70%\">Content</th><th>Author</th></tr>";
	
		foreach($comments as $comment){
			
			showComment($comment, $currUser, $start, $order);
		}		
	}
?>
</table>
<?php

	if($totalRows >PAGE_SIZE){
		echo "<table class=\"npPages\"><tr><th>";
		if($start!=0){
			echo "<a href=\"?postId=".$post->getValue("id")."&start=0&amp;order=$order\">First Page</a>";
			echo "&nbsp;&nbsp;";
		}
		if($start>0){
			echo "<a href=\"?postId=".$post->getValue("id")."&start="
						.max($start-PAGE_SIZE,0)
						."&amp;order=$order\">Previous Page</a>";	
		}
		echo "&nbsp;&nbsp;";
		if($start+PAGE_SIZE < $totalRows){
			echo "<a href=\"?postId=".$post->getValue("id")."&start="
						.min($start+PAGE_SIZE,$totalRows)
						."&amp;order=$order\">Next Page</a>";	
		}
		if($start<$totalRows-PAGE_SIZE){
			echo "&nbsp;&nbsp;";
			echo "<a href=\"?postId=".$post->getValue("id")."&start=".($totalRows-PAGE_SIZE)."&amp;order=$order\">Last Page</a>";
		}
		echo "</th></tr></table>";
	}
?>


<h2>Add New Comment</h2>
<form action="" method="post" enctype="multipart/form-data" name="createPost">
<table width="300" border="0">
  <tr>
    <td>Content</td>
    <td><textarea name="content"  class="ckeditor" cols="80" rows="10"></textarea></td>
    </tr>
</table><br>
<center><input type="submit" name="submit"></center>

</form>

</div>

</body>
</html>